package com.example.studentapp.course;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.studentapp.R;
import com.example.studentapp.db.SQLiteDB;

public class CourseAddActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView course_returnIV;
    TextView course_add11TV,course_add12TV,course_add13TV,course_add14TV,course_saveTV;
    TextView course_add21TV,course_add22TV,course_add23TV,course_add24TV;
    TextView course_add31TV,course_add32TV,course_add33TV,course_add34TV;
    TextView course_add41TV,course_add42TV,course_add43TV,course_add44TV;
    TextView course_add51TV,course_add52TV,course_add53TV,course_add54TV;
    EditText star_yearET,end_yearET,termET;
    SQLiteDB sqLiteDB;
    SQLiteDatabase sqLiteDatabase;
    int course_list_id = 0;
    String coursenName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_add);
        init();
    }

    private void init() {
        course_returnIV = findViewById(R.id.course_returnIV);

        star_yearET= findViewById(R.id.star_yearET);
        end_yearET= findViewById(R.id.end_yearET);
        termET= findViewById(R.id.termET);
        course_saveTV = findViewById(R.id.course_saveTV);

        course_add11TV = findViewById(R.id.course_add11TV);
        course_add12TV = findViewById(R.id.course_add12TV);
        course_add13TV = findViewById(R.id.course_add13TV);
        course_add14TV = findViewById(R.id.course_add14TV);

        course_add21TV = findViewById(R.id.course_add21TV);
        course_add22TV = findViewById(R.id.course_add22TV);
        course_add23TV = findViewById(R.id.course_add23TV);
        course_add24TV = findViewById(R.id.course_add24TV);

        course_add31TV = findViewById(R.id.course_add31TV);
        course_add32TV = findViewById(R.id.course_add32TV);
        course_add33TV = findViewById(R.id.course_add33TV);
        course_add34TV = findViewById(R.id.course_add34TV);

        course_add41TV = findViewById(R.id.course_add41TV);
        course_add42TV = findViewById(R.id.course_add42TV);
        course_add43TV = findViewById(R.id.course_add43TV);
        course_add44TV = findViewById(R.id.course_add44TV);

        course_add51TV = findViewById(R.id.course_add51TV);
        course_add52TV = findViewById(R.id.course_add52TV);
        course_add53TV = findViewById(R.id.course_add53TV);
        course_add54TV = findViewById(R.id.course_add54TV);
        course_returnIV.setOnClickListener(this);

        course_add11TV.setOnClickListener(this);
        course_add12TV.setOnClickListener(this);
        course_add13TV.setOnClickListener(this);
        course_add14TV.setOnClickListener(this);

        course_add21TV.setOnClickListener(this);
        course_add22TV.setOnClickListener(this);
        course_add23TV.setOnClickListener(this);
        course_add24TV.setOnClickListener(this);

        course_add31TV.setOnClickListener(this);
        course_add32TV.setOnClickListener(this);
        course_add33TV.setOnClickListener(this);
        course_add34TV.setOnClickListener(this);

        course_add41TV.setOnClickListener(this);
        course_add42TV.setOnClickListener(this);
        course_add43TV.setOnClickListener(this);
        course_add44TV.setOnClickListener(this);

        course_add51TV.setOnClickListener(this);
        course_add52TV.setOnClickListener(this);
        course_add53TV.setOnClickListener(this);
        course_add54TV.setOnClickListener(this);
        course_saveTV.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.course_returnIV:
                finish();
                break;
            case R.id.course_saveTV:
                // 添加课程表
                String star_year = star_yearET.getText().toString();
                String end_year = end_yearET.getText().toString();
                String term = termET.getText().toString();
                if(star_year.length()>0 && end_year.length()>0 && term.length()>0){
                    if(Integer.parseInt(end_year) - Integer.parseInt(star_year) == 1){
                        // 入库
                        try{
                            sqLiteDB = new SQLiteDB(getApplicationContext());
                            sqLiteDatabase = sqLiteDB.getWritableDatabase();
                            coursenName = star_year+getString(R.string.course_sign)+end_year+
                                    getString(R.string.course_year)+term+getString(R.string.course_term);
                            String insertSql="insert into course_list(name) values('"+coursenName+"')";
                            sqLiteDatabase.execSQL(insertSql);
                            // 设置为只读
                            star_yearET.setEnabled(false);
                            end_yearET.setEnabled(false);
                            termET.setEnabled(false);
                        }catch (Exception e){
                            Toast.makeText(getApplicationContext(), getString(R.string.course_add_error), Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        Toast.makeText(getApplicationContext(),getString(R.string.course_year_error),Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(getApplicationContext(),getString(R.string.course_toast_null),Toast.LENGTH_SHORT).show();
                }
                break;
                // 点击课程 空白处理 弹出对话框
            case R.id.course_add11TV:
                callDialog(course_add11TV,11);
                break;
            case R.id.course_add12TV:
                callDialog(course_add12TV,12);
                break;
            case R.id.course_add13TV:
                callDialog(course_add13TV,13);
                break;
            case R.id.course_add14TV:
                callDialog(course_add14TV,14);
                break;


            case R.id.course_add21TV:
                callDialog(course_add21TV,21);
                break;
            case R.id.course_add22TV:
                callDialog(course_add22TV,22);
                break;
            case R.id.course_add23TV:
                callDialog(course_add23TV,23);
                break;
            case R.id.course_add24TV:
                callDialog(course_add24TV,24);
                break;

            case R.id.course_add31TV:
                callDialog(course_add31TV,31);
                break;
            case R.id.course_add32TV:
                callDialog(course_add32TV,32);
                break;
            case R.id.course_add33TV:
                callDialog(course_add33TV,33);
                break;
            case R.id.course_add34TV:
                callDialog(course_add34TV,34);
                break;

            case R.id.course_add41TV:
                callDialog(course_add41TV,41);
                break;
            case R.id.course_add42TV:
                callDialog(course_add42TV,42);
                break;
            case R.id.course_add43TV:
                callDialog(course_add43TV,43);
                break;
            case R.id.course_add44TV:
                callDialog(course_add44TV,44);
                break;

            case R.id.course_add51TV:
                callDialog(course_add51TV,51);
                break;
            case R.id.course_add52TV:
                callDialog(course_add52TV,52);
                break;
            case R.id.course_add53TV:
                callDialog(course_add53TV,53);
                break;
            case R.id.course_add54TV:
                callDialog(course_add54TV,54);
                break;

        }
    }

    @SuppressLint("Range")
    private void callDialog(TextView courseTV, int section) {
        //获取添加后的course_list_id
        String sql = "select * from course_list where name='" + coursenName + "'";
        Cursor cursor = sqLiteDatabase.rawQuery(sql,null);
        if(cursor.getCount() > 0){
            cursor.moveToFirst();
            course_list_id = cursor.getInt(cursor.getColumnIndex("id"));
        }else{
            Toast.makeText(getApplicationContext(), getString(R.string.course_id_error), Toast.LENGTH_SHORT).show();
        }
        // Dialog 弹出框
        Dialog myDialog = new Dialog(CourseAddActivity.this);
        // 不要标题
        myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = myDialog.getWindow();
        window.setContentView(R.layout.add_course_dialog);
        myDialog.show();
        EditText course_nameET = window.findViewById(R.id.course_nameET);
        EditText course_placeET=window.findViewById(R.id.course_placeET);
        EditText course_teaET=window.findViewById(R.id.course_teaET);
        Button course_okBtn =window.findViewById(R.id.course_okBtn);
        Button course_cancelBtn =window.findViewById(R.id.course_cancelBtn);

        course_cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myDialog.dismiss();
            }
        });
        course_okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name =course_nameET.getText().toString();// 课程名
                String place = course_placeET.getText().toString(); // 上课地点
                String tea = course_teaET.getText().toString(); // 老师
                if(name.length()>0&&place.length()>0&&tea.length()>0){
                    try{
                        String insertSql="insert into course_detail(course_list_id,name,place,teacher,section) values("+course_list_id+",'"+name+"','"+
                                place+"','"+tea+"',"+section+")";
                        sqLiteDatabase.execSQL(insertSql);
                        courseTV.setText(name);
                        myDialog.dismiss();
                    }catch (Exception e){
                        Toast.makeText(getApplicationContext(),
                                getString(R.string.course_add_detail_error),Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(getApplicationContext(),getString(R.string.course_toast_no),Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}